package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.dao.AdvertiseTypeDAO;
import com.junzhiit.nengji.shop.model.AdvertiseType;
import com.junzhiit.nengji.shop.model.enums.isMobile;

@Repository
public class AdvertiseTypeDAOImpl extends AbstractBaseDAO<AdvertiseType> implements AdvertiseTypeDAO {

	@Override
	public List<AdvertiseType> findAll(int pageNumber, int maxPageRow, AdvertiseType advertiseType) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from `advertise_type` where is_delete=? and is_mobile =?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(isMobile.WAP.toIntValue());

		if (StringUtils.isNotBlank(advertiseType.getName())) {
			sql.append(" and name like ?");
			params.setString("%" + advertiseType.getName() + "%");
		}

		return queryForPagingList(sql.toString(), AdvertiseType.class, params, pageNumber, maxPageRow);
	}

	@Override
	public Integer getTotalCount(AdvertiseType advertiseType) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from `advertise_type` where is_delete=? and is_mobile =?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(isMobile.WAP.toIntValue());

		if (StringUtils.isNotBlank(advertiseType.getName())) {
			sql.append(" and name like ?");
			params.setString("%" + advertiseType.getName() + "%");
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public Integer getAdvertiseTypeExist(AdvertiseType advertiseType) {
		StatementParameter params = new StatementParameter();

		StringBuffer sql = new StringBuffer();

		sql.append("select count(*) from `advertise_type` where is_delete=?");
		params.setInt(IsDelete.NO.toIntValue());

		if (StringUtils.isNotBlank(advertiseType.getName())) {
			sql.append(" and name=?");
			params.setString(advertiseType.getName());
		}

		return queryForInt(sql.toString(), params);
	}

	@Override
	public String getMaxCode() {
		String sql = "select max(code) from `advertise_type`";

		return queryForString(sql.toString());
	}

	@Override
	public List<AdvertiseType> getList() {
		StringBuffer sql = new StringBuffer();
		sql.append("select id,name from `advertise_type` where is_delete =?");
		
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		
		return super.queryForList(sql.toString(), AdvertiseType.class, params);
	}

}
